// pages/user/shop-statistic/index.js
const app = getApp()
const api  = require('../../../api/user')
const moment = require('../../../utils/moment.min')
import {initLoadMoreMixin} from '../../../utils/list-load-more'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabItems: [{
      title: '今日销量',
      status: 0
    }, {
      title: '昨日销量',
      status: 3
    }, {
      title: '按时间',
      status: 4
    }],
    shopId: app.getUser().id,
    user: app.getUser(),
    activeIndex: 0,
    statisticType: '0',
    priceTotal: 0,
    numTotal: 0,
    btnStyle: {
        'background-color': 'white'
    },
    startDate: moment(new Date()).format("YYYY-MM-DD"),
    endDate: moment(new Date()).format("YYYY-MM-DD"),
    showPickDate: false,
    items: [],
    isEmpty: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    initLoadMoreMixin.call(this)
    this.resetInit()
  },
  initData() {
    const timeType = this.data.tabItems[this.data.activeIndex].status;
    return app.requestUrl({
      url: api.COMMANDER_STATISTIC, 
      data:{
        mid: this.data.shopId,
        centreId: app.globalData.loginCentreId,
        timeType: timeType,
        type: this.data.statisticType,
        start: this.data.startDate,
        end: this.data.endDate,
        offset: (this.data.pages.currPage - 1) * this.data.pages.perPage,
        limit: this.data.pages.perPage
      }
      }
    ).then(res => {
      const {
        countList,
        salesTotal
      } = res.data.data;
     
      this.setData({
          items: this.data.items.concat(countList.rows),
          priceTotal: (salesTotal.totalPrice || 0).toFixed(2),
          numTotal:salesTotal.total || 0})
      return {
        pageCount: countList.rows.length,
        totalCount: countList.total
      };
    });
  },
  handleTabClick(e) {
      this.setData({activeIndex: e.detail.index})
      //this.activeIndex = index;
      this.resetInit();
  },
  onTypeClick(e) {

    this.setData({statisticType: e.currentTarget.dataset.type})
     this.resetInit();
  },
  onDatePicked(e) {
    console.log('onDatePicked',e)
    this.setData({
      startDate: moment(e.detail.startDate).format("YYYY-MM-DD"), 
      endDate: moment(e.detail.endDate).format("YYYY-MM-DD"),
      showPickDate: false
    })
     this.resetInit();
  },
  onDateCancel() {
      this.setData({
        showPickDate: false
      })
  },
  bindShowPicker () {
    this.setData({
      showPickDate: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})